package com.atuanzhang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.atuanzhang.R;
import com.atuanzhang.bean.Goods;
import com.atuanzhang.image.AsyncImageLoader;
import com.atuanzhang.image.AsyncImageLoader.ImageCallback;

public class GoodsListAdapter extends BaseAdapter{
	
	private final Context context;
	
	private final List<Goods> goodsList;
	
	private AsyncImageLoader asyncImageLoader;
	
	public  GoodsListAdapter(Context context, List<Goods> goodsList){
	
		this.context = context;
		
		this.goodsList = goodsList;
		
		this.asyncImageLoader = new AsyncImageLoader();
	}

	@Override
	public int getCount() {
		return goodsList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Goods goods = goodsList.get(position);
		
		convertView = LayoutInflater.from(context).inflate(R.layout.android_good, null);
		
		String url = goods.getLoc();
		//url
		TextView urlsiteTextView = (TextView) convertView.findViewById(R.id.goods_url_text_view);
		urlsiteTextView.setTag(url);
		
		//商品所属团购网
		TextView websiteTextView = (TextView) convertView.findViewById(R.id.goods_website_text_view);
		websiteTextView.setText("【"+goods.getWebsite()+"】");
		
		// 标题
		TextView titleTextView = (TextView) convertView.findViewById(R.id.goods_title_text_view);
		titleTextView.setText(getStr(goods.getTitle()));
		
		//价格
		TextView priceTextView = (TextView) convertView.findViewById(R.id.goods_pirce_text_view);
		priceTextView.setText("￥"+goods.getPrice());
		
		//原价
		TextView valueTextView = (TextView) convertView.findViewById(R.id.goods_value_text_view);
		valueTextView.setText("￥"+goods.getValue());
		valueTextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
		
		//折扣
		TextView discountTextView = (TextView) convertView.findViewById(R.id.goods_discount_text_view);
		String discount = getDiscount(goods.getPrice(), goods.getValue());
		discountTextView.setText(discount+"折");
		
		// Load the image and set it on the ImageView
		String imageUrl = goods.getImage();
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.goods_image_view);
        
        imageView.setTag(imageUrl);
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = imageView;
                if (imageViewByTag != null) {
                    imageViewByTag.setImageDrawable(imageDrawable);
                }
            }
        });
        
		if (cachedImage != null) {
			imageView.setImageDrawable(cachedImage);
		}

		return convertView;
	}
	
	
	private String getDiscount(double price, double value){
		
		String b = "0";
		
		if(value > 0.01f){
			double a = price/value*100;  
			b = String.valueOf(Math.round(a) /10f);

		}
		
		return b;
	}
	
	private String getStr(String title){
		
		if(title.length()>40){
			return title.substring(0, 40)+"...";
		}else{
			return title;
		}
		
	}

}
